//获取应用实例
var app = getApp()
var Bmob = require("../../utils/bmob.js");
var common = require('../template/getCode.js')
var that;
var myDate = new Date();
//格式化日期
function formate_data(myDate) {
  let month_add = myDate.getMonth() + 1;
  var formate_result = myDate.getFullYear() + '-' +
    month_add + '-' +
    myDate.getDate()
  return formate_result;
}
var count = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    day: 0, //在一起的第多少天

    stars: [0, 1, 2, 3, 4],
    normalSrc: '../../static/images/no-star.png',
    selectedSrc: '../../static/images/full-star.png',
    halfSrc: '../../static/images/half-star.png',
    key:2, //评分

    notice_status: false,
    accounts: ["微信号", "QQ号", "手机号"],
    accountIndex: 0,
    peopleHide: false,
    isAgree: false,
    date: formate_data(myDate),
    address: '点击选择位置',
    longitude: 0, //经度
    latitude: 0, //纬度
    showTopTips: false,
    TopTips: '',
    noteMaxLen: 200, //备注最多字数
    content: "",
    noteNowLen: 0, //备注当前字数
    types: ["运动", "游戏", "交友", "旅行", "读书", "竞赛", "电影", "音乐", "其他"],
    typeIndex: "0",
    showInput: false, //显示输入真实姓名,
  },

  //点击左边,半颗星
  selectLeft: function(e) {

    var key = e.currentTarget.dataset.key
    if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {
      //只有一颗星的时候,再次点击,变为0颗
      key = 0;
    }
    count = key
    this.setData({
      key: key
    })
    console.log(key)

  },
  //点击右边,整颗星
  selectRight: function(e) {
  
    var key = e.currentTarget.dataset.key
    count = key
    this.setData({
      key: key
    })
    console.log(key)
    
  },

  tapNotice: function(e) {
    if (e.target.id == 'notice') {
      this.hideNotice();
    }
  },
  showNotice: function(e) {
    this.setData({
      'notice_status': true
    });
  },
  hideNotice: function(e) {
    this.setData({
      'notice_status': false
    });
  },


  //字数改变触发事件
  bindTextAreaChange: function(e) {
    var that = this
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    that.setData({
      content: value,
      noteNowLen: len
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    that.setData({ //初始化数据
      src: "",
      isSrc: false,
      ishide: "0",
      autoFocus: true,
      isLoading: false,
      loading: true,
      isdisabled: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.hideToast()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var myInterval = setInterval(getReturn, 500); ////半秒定时查询
    function getReturn() {
      wx.getStorage({
        key: 'user_openid',
        success: function(ress) {
          if (ress.data) {
            clearInterval(myInterval)
            that.setData({
              loading: true
            })
          }
        }
      })
    }
  },

  //上传活动图片
  uploadPic: function() { //选择图标
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], //压缩图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          isSrc: true,
          src: tempFilePaths
        })
      }
    })
  },

  //删除图片
  clearPic: function() { //删除图片
    that.setData({
      isSrc: false,
      src: ""
    })
  },




  //表单验证
  showTopTips: function() {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  //提交表单
  submitForm: function(e) {
    var that = this;
    console.log(e)

    // if (that.data.showInput == false) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请先阅读《发起须知》'
    //   })
    //   return;
    // }
    var title = e.detail.value.title;
    var content = e.detail.value.content;


    that.setData({
      isLoading: true,
      isdisabled: true
    })
    //向 Events 表中新增一条数据
    var saveInfo = function(res) {
      var Diary = Bmob.Object.extend("everyDayInfo");
      var diary = new Diary();
      var me = new Bmob.User();
      //me.id = ress.data;

      diary.set("title", title);
      diary.set("date", new Date());
      diary.set("longitude", "0"); //经度
      diary.set("latitude", "0"); //纬度\

      diary.set("content", content);
      if (that.data.isSrc == true) {
        var name = that.data.src; //上传图片的别名
        var file = new Bmob.File(name, that.data.src);
        file.save();
        diary.set("pic", file);
      }
      //新增操作
      diary.save(null, {
        success: function(result) {

          console.log("发布成功,objectId:" + result.id);
          that.setData({
            isLoading: false,
            isdisabled: false,
            eventId: result.id,
          })
          //添加成功，返回成功之后的objectId(注意，返回的属性名字是id,而不是objectId)
          common.dataLoading("发起成功", "success", function() {
            //重置表单

          });
        },
        error: function(result, error) {
          //添加失败
          console.log("发布失败=", error);
          common.dataLoading("发起失败", "loading");
          that.setData({
            isLoading: false,
            isdisabled: false
          })
        }
      })
    };
    saveInfo();
    // wx.getStorage({
    //   key: 'user_id',
    //   success:function(res){
    //     console.log(res,789);
    //     saveInfo(res);

    //   },
    // })


    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})

//根据活动类型获取活动类型名称
function getTypeName(acttype) {
  var acttypeName = "";
  if (acttype == 1) acttypeName = "运动";
  else if (acttype == 2) acttypeName = "游戏";
  else if (acttype == 3) acttypeName = "交友";
  else if (acttype == 4) acttypeName = "旅行";
  else if (acttype == 5) acttypeName = "读书";
  else if (acttype == 6) acttypeName = "竞赛";
  else if (acttype == 7) acttypeName = "电影";
  else if (acttype == 8) acttypeName = "音乐";
  else if (acttype == 9) acttypeName = "其他";
  return acttypeName;
}